import 'dart:collection';

import 'package:dio/dio.dart';
import 'package:tender_flutter/constant/api_path.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/http/api_response.dart';
import 'package:tender_flutter/http/http_utils.dart';

class ApiPerson {
  /// 获取用户信息
  static Future getUserInfo<T>() async {
    try {
      final response = await HttpUtils.post(ApiPath.userInfo);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 更新用户信息
  static Future updateUserInfo<T>(
      {int areaCode,
      String company,
      String phone,
      String avator,
      int sex}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["area_id"] = areaCode;
      params["company"] = company;
      params["phone"] = phone;
      params["picture"] = avator;
      params["sex"] = sex;
      final response =
          await HttpUtils.postForm(ApiPath.updateUserInfo, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 获取会员套餐
  static Future getMemberCombo<T>() async {
    try {
      final response = await HttpUtils.post(ApiPath.memberCombo);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 我的消息
  static Future getMyNews<T>(
      {int page = AppConfig.page, int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.personalNews, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 我的收藏
  /// 1招投标关注,2企业关注,3供需关注,4资质关注,5供应商
  static Future getMyCollection<T>(
      {int type,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["type"] = type;
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.personalCollection, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 我发布的供需
  /// type :0所有1供2需;
  static Future getMyMarket<T>(
      {String beginTime,
      String endTime,
      int type,
      int page = AppConfig.page,
      int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["begin"] = beginTime;
      params["end"] = endTime;
      params["type"] = type;
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.personalMarket, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 删除供需
  static Future deleteMarket<T>({int marketId}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = marketId;
      final response =
          await HttpUtils.get(ApiPath.deleteMarket, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 消费记录列表
  static Future getConsumptionList<T>(
      {int page = AppConfig.page, int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.consumptionList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 提现历史
  static Future getTiXianHistory<T>(
      {int page = AppConfig.page, int pageCount = AppConfig.pageCount}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["page"] = page;
      params["size"] = pageCount;
      final response =
          await HttpUtils.get(ApiPath.tixianHistoryList, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 提现
  static Future tiXian<T>({String money, String account}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["money"] = money;
      params["account"] = account;
      final response = await HttpUtils.get(ApiPath.tixian, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 申请发票
  /// fpType:1企业2个人
  static Future applyInvoice<T>(
      {String id,
      int fpType,
      String address,
      String blank,
      String blankNum,
      String code,
      String email,
      String phone,
      String remark,
      String title}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["id"] = id;
      params["fpType"] = fpType;
      params["address"] = address;
      params["bank"] = blank;
      params["bankNum"] = blankNum;
      params["code"] = code;
      params["email"] = email;
      params["phone"] = phone;
      params["remark"] = remark;
      params["title"] = title;
      final response =
          await HttpUtils.get(ApiPath.applyInvoice, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 关于我们
  static Future getAboutUsInfo<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.aboutUsInfo);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 意见反馈
  static Future feedback<T>({String content, List<String> pics}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["content"] = content;
      params["pics"] = pics;
      final response =
          await HttpUtils.postRaw(ApiPath.feedback, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 邀请海报
  static Future getInvitePoster<T>() async {
    try {
      final response = await HttpUtils.get(ApiPath.invitePoster);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }

  /// 绑定邀请码
  static Future bindingInvite<T>({String invite}) async {
    try {
      Map<String, dynamic> params = HashMap();
      params["inviteCode"] = invite;
      final response = await HttpUtils.post(ApiPath.inviteBind, params: params);
      return ApiResponse<T>.completed(response);
    } on DioError catch (e) {
      return ApiResponse.error(e.error);
    }
  }
}
